package com.xuecheng.media.api;

import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @description 大文件上传接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "大文件上传接口",tags = "大文件上传接口")
 @RestController
public class BigFilesController {


  @Autowired
  MediaFileService mediaFileService;


     @ApiOperation(value = "文件上传前检查文件")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkfile(
            @RequestParam("fileMd5") String fileMd5
    ) throws Exception {
         return mediaFileService.checkFile(fileMd5);
    }


    @ApiOperation(value = "分块文件上传前的检测")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                            @RequestParam("chunk") int chunk) throws Exception {
        return mediaFileService.checkChunk(fileMd5,chunk);
    }

    @ApiOperation(value = "上传分块文件")
    @PostMapping("/upload/uploadchunk")
    public RestResponse uploadchunk(@RequestParam("file") MultipartFile file,
                                    @RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("chunk") int chunk) throws Exception {
        return mediaFileService.uploadChunk(fileMd5,chunk,file.getBytes());
    }

    @ApiOperation(value = "合并文件")
    @PostMapping("/upload/mergechunks")
    public RestResponse mergechunks(@RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("fileName") String fileName,
                                    @RequestParam("chunkTotal") int chunkTotal) throws Exception {
        Long companyId = 1232141425L;
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        //   获取扩展名
        String extension = null;
        String filename = fileName;
        if (StringUtils.isNoneBlank(filename) && filename.lastIndexOf(".")>=0) {
            extension = filename.substring(filename.indexOf("."));
        }
//   媒体类型
        String mimeTypeByextension = getMimeTypeByextension(extension);
//   图片  mp4 可以设置url
        if (mimeTypeByextension.indexOf("image")>=0 ){
            uploadFileParamsDto.setFileType("001001");
            uploadFileParamsDto.setTags("课程图片");
        }else {
            uploadFileParamsDto.setFileType("001002");
            uploadFileParamsDto.setTags("课程视频");
        }
        uploadFileParamsDto.setRemark("");
        uploadFileParamsDto.setFilename(fileName);
        ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(fileName);
        String mimeType = extensionMatch.getMimeType();
        uploadFileParamsDto.setContentType(mimeType);
        return mediaFileService.mergechunks(companyId,fileMd5,chunkTotal,uploadFileParamsDto);
    }
    // 根据扩展名匹配媒体类型
    private String getMimeTypeByextension(String extension){
        //   资源的媒体类型
        String contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;  // 默认未知二进制流
        //  取objectName中的扩展名  c.jpg
        ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
        if (extensionMatch!=null) {
            contentType = extensionMatch.getMimeType();
        }
        return contentType;
    }
}
